#
# @lc app=leetcode.cn id=14 lang=python3
#
# [14] 最长公共前缀
#
# https://leetcode-cn.com/problems/longest-common-prefix/description/
#
# algorithms
# Easy (38.47%)
# Likes:    1179
# Dislikes: 0
# Total Accepted:    318.1K
# Total Submissions: 826.9K
# Testcase Example:  '["flower","flow","flight"]'
#
# 编写一个函数来查找字符串数组中的最长公共前缀。
#
# 如果不存在公共前缀，返回空字符串 ""。
#
# 示例 1:
#
# 输入: ["flower","flow","flight"]
# 输出: "fl"
#
#
# 示例 2:
#
# 输入: ["dog","racecar","car"]
# 输出: ""
# 解释: 输入不存在公共前缀。
#
#
# 说明:
#
# 所有输入只包含小写字母 a-z 。
#
#

# @lc code=start

# 44 ms
class Solution:
    def longestCommonPrefix(self, strs: List[str]) -> str:
        if strs == None or len(strs) == 0 or strs[0] == None or strs[0] == "":
            return ""
        if len(strs) == 1:
            return strs[0]

        result = ""
        i = 0

        while True:
            if len(strs[0]) == i:
                return result
            current = strs[0][i]
            for string in strs:
                if len(string) == i or current != string[i]:
                    return result
            result = result + current
            i = i + 1

        return result

# @lc code=end

# 52 ms
# class Solution:
#     def longestCommonPrefix(self, strs: List[str]) -> str:
#         if strs == None or len(strs) == 0:
#             return ""
#         if len(strs) == 1:
#             return strs[0]

#         result = None

#         for string in strs:
#             if string == None or string == "" or result == "":
#                 return ""

#             if result == None:
#                 result = string
#                 continue

#             temp = ""
#             for i in range(0, min(len(result), len(string))):
#                 if result[i] == string[i]:
#                     temp = temp + result[i]
#                 else:
#                     break

#             result = temp

#         return result

# 46 ms
# class Solution:
#     def longestCommonPrefix(self, strs: List[str]) -> str:
#         if strs == None or len(strs) == 0:
#             return ""
#         if len(strs) == 1:
#             return strs[0]

#         result = ""
#         minLength = sys.maxsize

#         for string in strs:
#             if string == "" or len(string) == 0:
#                 return ""
#             if len(string) < minLength:
#                 minLength = len(string)

#         for i in range(0, minLength):
#             current = strs[0][i]
#             for string in strs:
#                 if current != string[i]:
#                     return result
#             result = result + current

#         return result
